package com.xcx.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xcx.commonuntils.returnuntls.ReturnMessage;
import com.xcx.commonuntils.returnuntls.ReturnMessageUtils;
import com.xcx.eduservice.entity.EduTeacher;
import com.xcx.eduservice.mapper.EduTeacherMapper;
import com.xcx.eduservice.request.TeacherQuery;
import com.xcx.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-10-08
 */
@Service
@Primary
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {
    final private EduTeacherMapper eduTeacherMapper;
    @Override
    public ReturnMessage<IPage<EduTeacher>> pageTeacherCondition(TeacherQuery request) {



        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        String name = request.getName();
        Integer level = request.getLevel();
        String begin = request.getBegin();
        String end = request.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if (!StringUtils.isEmpty(name)) {
            //构建条件
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create", end);
        }


        Integer count = eduTeacherMapper.selectCount(wrapper);

        IPage<EduTeacher> page = new Page<>(request.getPageNo(), request.getPageSize(), count);

        IPage<EduTeacher> eduTeacherIPage = eduTeacherMapper.selectPage(page, wrapper);


        return ReturnMessageUtils.success(eduTeacherIPage);
    }
}
